package com.cxh.rmsq.ui.activity.self;

import static com.cxh.rmsq.utils.ExtendsKt.loadImage;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cxh.rmsq.R;
import com.cxh.rmsq.common.IntentExtra;
import com.cxh.rmsq.db.model.BaseResultListBean;
import com.cxh.rmsq.db.model.CommunityContent;
import com.cxh.rmsq.db.model.MineBean;
import com.cxh.rmsq.im.IMManager;
import com.cxh.rmsq.model.AddFriendResult;
import com.cxh.rmsq.model.GroupMember;
import com.cxh.rmsq.model.Resource;
import com.cxh.rmsq.model.Resource2;
import com.cxh.rmsq.model.Result2;
import com.cxh.rmsq.model.Status;
import com.cxh.rmsq.net.RetrofitUtil;
import com.cxh.rmsq.net.newNet.BaseResultEntity;
import com.cxh.rmsq.net.newNet.ProjectViewModel;
import com.cxh.rmsq.ui.activity.GroupDetailActivity;
import com.cxh.rmsq.ui.adapter.self.CircleListAdapter;
import com.cxh.rmsq.ui.dialog.CommonDialog;
import com.cxh.rmsq.ui.dialog.SimpleInputDialog;
import com.cxh.rmsq.ui.widget.SelectableRoundedImageView;
import com.cxh.rmsq.utils.GlideEngine;
import com.cxh.rmsq.utils.MyUtils;
import com.cxh.rmsq.utils.ToastUtils;
import com.cxh.rmsq.viewmodel.JoinGroupViewModel;
import com.cxh.rmsq.viewmodel.SearchFriendNetViewModel;
import com.cxh.rmsq.viewmodel.SocialCircleViewModel;
import com.cxh.rmsq.viewmodel.UserDetailViewModel;
import com.cxh.rmsq.viewmodel.UserInfoViewModel;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.hjq.shape.view.ShapeTextView;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import io.rong.imkit.RongIM;
import io.rong.imkit.userinfo.RongUserInfoManager;
import io.rong.imkit.utils.RouteUtils;
import io.rong.imlib.model.ConversationIdentifier;
import io.rong.imlib.model.UserInfo;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import okhttp3.RequestBody;

public class LookUserDetailActivity2 extends AppCompatActivity implements View.OnClickListener, CircleListAdapter.OnCustomClickListener {

    private LinearLayout clToolbar, llBottom;
    private RelativeLayout rl_top_black;
    private LinearLayout ivBack;
    private ImageView ivRightImage, ivRightImageBlack;
    private Banner banner;
    private NestedScrollView scrollView;
    private ShapeTextView stvEdit, stvOrder, stvSend, stvAddBlack, stvAddFriend;
    private SelectableRoundedImageView siv_header;
    private TextView tvName, tvSexAge, tvFun, tvZan, tvCircleCount;
    private RecyclerView tvIp, recyclerView;
    private TextView tvNone;
    private ImageView sll_vip;
    String targetId = "";
    String targetName = "";
    private List banner_data = new ArrayList();
    private UserInfoViewModel userInfoViewModel;
    private SocialCircleViewModel socialCircleViewModel;
    private CircleListAdapter listAdapter;
    private List<CommunityContent> dataList = new ArrayList<>();
    private List<CommunityContent> newData;
    private int pageNo = 1;
    private int pageSize = 100;
    private SearchFriendNetViewModel viewModel;
    private boolean isFriend;
    String isFollower = "";
    private ProjectViewModel viewProjectModel;
    private final ArrayList<LocalMedia> imagesMediaList = new ArrayList<>();
    String isContact;
    private UserDetailViewModel userDetailViewModel;
    /**
     * 是否在删除好友操作，如果在删除中则不更新好友状态，防止在删除时会有界面刷新
     */
    private boolean isInDeleteAction = false;
    private BasePopupView popupView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_user_detail);
        targetId = getIntent().getStringExtra(IntentExtra.STR_TARGET_ID).toString();

        viewModel = ViewModelProviders.of(this).get(SearchFriendNetViewModel.class);
        userDetailViewModel =
                ViewModelProviders.of(
                                this, new UserDetailViewModel.Factory(getApplication(), targetId))
                        .get(UserDetailViewModel.class);
        initView();
        initData();
    }

    private void initView() {
        clToolbar = findViewById(R.id.clToolbar);
        ivBack = findViewById(R.id.iv_back);
        findViewById(R.id.iv_back_black).setOnClickListener(this);
        ivRightImageBlack = findViewById(R.id.iv_right_image_black);
        ivRightImageBlack.setOnClickListener(this);
        ivRightImage = findViewById(R.id.iv_right_image);
        banner = findViewById(R.id.banner);
        tvName = findViewById(R.id.tv_name);
        tvSexAge = findViewById(R.id.tv_sex_age);
        tvFun = findViewById(R.id.tv_fun);
        tvZan = findViewById(R.id.tv_zan);
        stvEdit = findViewById(R.id.stv_edit);
        tvCircleCount = findViewById(R.id.tv_circleCount);
        tvIp = findViewById(R.id.tv_ip);
        recyclerView = findViewById(R.id.recyclerView);
        tvNone = findViewById(R.id.tv_none);
        stvSend = findViewById(R.id.stv_send);
        stvAddBlack = findViewById(R.id.stv_add_black);
        stvOrder = findViewById(R.id.stv_order);
        stvAddFriend = findViewById(R.id.stv_add_friend);
        scrollView = findViewById(R.id.scrollView);
        rl_top_black = findViewById(R.id.rl_top_black);
        llBottom = findViewById(R.id.ll_bottom);
        siv_header = findViewById(R.id.siv_header);
        sll_vip = findViewById(R.id.sll_vip);

        clToolbar.setPadding(0, 0, 0, 0);
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent));
        BarUtils.setStatusBarLightMode(this, true);

        ivBack.setOnClickListener(this);
        ivRightImage.setOnClickListener(this);

        stvEdit.setOnClickListener(this);
        stvSend.setOnClickListener(this);
        stvAddBlack.setOnClickListener(this);
        stvAddFriend.setOnClickListener(this);
        stvOrder.setOnClickListener(this);

        isContact = getIntent().getStringExtra("isContact");//2 联系人列表 1 社区相关
//        if (getIntent().getStringExtra("isMine").equals("1")) {
//            ivRightImage.setVisibility(View.GONE);
//            ivRightImageBlack.setVisibility(View.GONE);
//            stvEdit.setVisibility(View.VISIBLE);
//            llBottom.setVisibility(View.GONE);
//        } else {
//        }
        if (targetId.equals(IMManager.getInstance().getCurrentId())) {
            ivRightImage.setVisibility(View.GONE);
            ivRightImageBlack.setVisibility(View.GONE);
            stvEdit.setVisibility(View.VISIBLE);
            llBottom.setVisibility(View.GONE);
        } else {
            ivRightImage.setVisibility(View.VISIBLE);
            ivRightImageBlack.setVisibility(View.VISIBLE);
            stvEdit.setVisibility(View.GONE);
            llBottom.setVisibility(View.VISIBLE);
            viewModel.isFriend(targetId);
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        listAdapter = new CircleListAdapter(3, this);
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

            }
        });
        listAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()) {
                    case R.id.iv_like:
                        int type = 0;
                        if (dataList.get(position).isLiked() == 1) {//已点赞
                            type = 0;
                        } else {
                            type = 1;
                        }
                        HashMap<String, Object> paramsMap = new HashMap<>();
                        paramsMap.put("busId", dataList.get(position).getId());
                        paramsMap.put("userId", IMManager.getInstance().getCurrentId());
                        paramsMap.put("type", 1);//1动态2评论
                        paramsMap.put("isLiked", type);//1:点赞 0：取消
                        paramsMap.put("bdzUserId", targetId);//被点赞用户id,
                        RequestBody body = RetrofitUtil.createJsonRequest(paramsMap);
                        socialCircleViewModel.commentLiked(body);
                        int finalType = type;
                        socialCircleViewModel.commentLiked().observe(LookUserDetailActivity2.this, new Observer<Result2<String>>() {
                            @Override
                            public void onChanged(Result2<String> result) {
                                if (result.code == 200) {
//                                    if (finalType ==1){
//                                        ToastUtils.showToast("点赞成功");
//                                    }
                                    socialCircleViewModel.getToUserCommunityContentPage(IMManager.getInstance().getCurrentId(), targetId, pageNo, pageSize);
//                                    dataList.set(position, newData.get(position));
                                }
                            }
                        });
                        break;
                }
            }
        });
        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY > 40) {
                    rl_top_black.setVisibility(View.VISIBLE);
                } else {
                    rl_top_black.setVisibility(View.GONE);
                }
//                when {
//                    scrollHeight > scrollY -> {
//                        //滑动小于开始渐变的位置  标题栏为透明  图标及文字为白色
//                        setTitleViewAlpha(0f)
//                    }
//                    scrollY in scrollHeight..windowHeight -> {
//                        //alpha 为控件的透明程度  0完全透明  1 不透明
//                        var alpha = (scrollY - scrollHeight) * 1.0 / titlHeight
//                        setTitleViewAlpha(alpha.toFloat())
//                    }
//                else ->{
//                        //滑动大于完全显示的位置  标题栏为白色  图标及文字为深色
//                        setTitleViewAlpha(1f)
//                    }
//                }

            }
        });
    }

    private void showAddFriendDialog(String userId) {
        final EditText et = new EditText(this);
        SimpleInputDialog dialog = new SimpleInputDialog();
        dialog.setInputHint(getString(R.string.profile_add_friend_hint));
        dialog.setInputDialogListener(
                new SimpleInputDialog.InputDialogListener() {
                    @Override
                    public boolean onConfirmClicked(EditText input) {
                        String inviteMsg = input.getText().toString();
                        UserInfo userInfo =
                                RongUserInfoManager.getInstance()
                                        .getUserInfo(IMManager.getInstance().getCurrentId());
                        // 如果邀请信息为空则使用默认邀请语
                        if (TextUtils.isEmpty(inviteMsg) && userInfo != null) {
                            // 当有附带群组名时显示来自哪个群组，没有时仅带自己的昵称
                            inviteMsg =
                                    getString(
                                            R.string.profile_invite_friend_description_format,
                                            userInfo.getName());
                        }
                        viewModel.inviteFriend(userId, inviteMsg);
                        return true;
                    }
                });
        dialog.show(getSupportFragmentManager(), null);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }

    private void initData() {
        viewProjectModel = ViewModelProviders.of(this).get(ProjectViewModel.class);
        userInfoViewModel = ViewModelProviders.of(this).get(UserInfoViewModel.class);
        socialCircleViewModel = ViewModelProviders.of(this).get(SocialCircleViewModel.class);

        viewModel
                .getIsFriend()
                .observe(
                        this,
                        new Observer<Boolean>() {
                            @Override
                            public void onChanged(Boolean aBoolean) {
                                isFriend = aBoolean;
                            }
                        });
        viewModel
                .getAddFriend()
                .observe(
                        this,
                        new Observer<Resource<AddFriendResult>>() {
                            @Override
                            public void onChanged(
                                    Resource<AddFriendResult> addFriendResultResource) {
                                if (addFriendResultResource.status == Status.SUCCESS) {
                                    Toast.makeText(
                                                    LookUserDetailActivity2.this,
                                                    R.string.common_request_success,
                                                    Toast.LENGTH_SHORT)
                                            .show();
                                } else if (addFriendResultResource.status == Status.ERROR) {
                                    if (addFriendResultResource.code == 430) {
                                        if (SPUtils.getInstance().getInt("vipLevel", 0) == 0) {//未开通会员
                                            ToastUtils.showToast("功能使用次数已达上限，请升级套餐或明天再试!");
                                            popupView = new XPopup.Builder(LookUserDetailActivity2.this)
                                                    .hasNavigationBar(false)
                                                    .isViewMode(true)
                                                    .isDestroyOnDismiss(true)
                                                    .dismissOnTouchOutside(true)
                                                    .dismissOnBackPressed(true)
                                                    .asConfirm("提示", "请升级会员权限", "取消", "去开通", new OnConfirmListener() {
                                                        @Override
                                                        public void onConfirm() {
                                                            popupView.smartDismiss();
                                                            startActivity(new Intent(LookUserDetailActivity2.this, VIPOpenActivity.class));
                                                        }
                                                    }, new OnCancelListener() {
                                                        @Override
                                                        public void onCancel() {
                                                            popupView.smartDismiss();
                                                        }
                                                    }, false);
                                            popupView.show();
                                        } else {
                                            ToastUtils.showToast("功能使用次数已达上限!");
                                        }
                                    } else {
                                        Toast.makeText(
                                                        LookUserDetailActivity2.this,
                                                        String.format(
                                                                getString(
                                                                        R.string
                                                                                .seal_quest_failed_error_code),
                                                                addFriendResultResource.code),
                                                        Toast.LENGTH_SHORT)
                                                .show();
                                    }

                                }
                            }
                        });
        // 获取添加到黑名单结果
        userDetailViewModel
                .getAddBlackListResult()
                .observe(
                        this,
                        new Observer<Resource<Void>>() {
                            @Override
                            public void onChanged(Resource<Void> resource) {
                                if (!isInDeleteAction) {
                                    if (resource.status == Status.SUCCESS) {
                                        ToastUtils.showToast(R.string.common_add_successful);
                                    } else if (resource.status == Status.ERROR) {
                                        ToastUtils.showToast(resource.message);
                                    }
                                }
                            }
                        });
        // 获取删除好友
        userDetailViewModel
                .getDeleteFriendResult()
                .observe(
                        this,
                        new Observer<Resource<Void>>() {
                            @Override
                            public void onChanged(Resource<Void> resource) {
                                if (resource.status == Status.SUCCESS) {
                                    ToastUtils.showToast(R.string.common_delete_successful);
                                    // 删除成功后关闭界面
                                    finish();
                                } else if (resource.status == Status.ERROR) {
                                    // 删除失败时置回删除行为
                                    isInDeleteAction = false;
                                    ToastUtils.showToast(resource.message);
                                }
                            }
                        });


        userInfoViewModel.requestUserSelfInfo(targetId);
        userInfoViewModel.getUserSelfInfo().observe(this, new Observer<Resource2<MineBean>>() {
            @Override
            public void onChanged(Resource2<MineBean> it) {
                if (it.data != null) {
                    isFollower = it.data.isFollower();
                    if ("2".equals(isContact)) {//联系人进来的
                        stvAddBlack.setVisibility(View.VISIBLE);
                        stvSend.setVisibility(View.VISIBLE);
                        stvOrder.setVisibility(View.GONE);
                        stvAddFriend.setVisibility(View.GONE);
                    } else {
                        if (isFriend) {
                            stvSend.setVisibility(View.VISIBLE);
                            if (Objects.equals(it.data.isFollower(), "1")) {//已关注
                                stvOrder.setVisibility(View.VISIBLE);
                                stvOrder.setText("已订阅");
                            } else {
                                stvOrder.setVisibility(View.VISIBLE);
                                stvOrder.setText("+ 订阅");
                            }
                            stvOrder.setVisibility(View.GONE);
                            stvAddBlack.setVisibility(View.VISIBLE);
                            stvAddFriend.setVisibility(View.GONE);
                        } else {
                            stvAddFriend.setVisibility(View.VISIBLE);
                            if (Objects.equals(it.data.isFollower(), "1")) {//已关注
                                stvOrder.setVisibility(View.VISIBLE);
                                stvOrder.setText("已订阅");
                            } else {
                                stvOrder.setVisibility(View.VISIBLE);
                                stvOrder.setText("+ 订阅");
                            }
                            stvOrder.setVisibility(View.GONE);
                            stvAddBlack.setVisibility(View.GONE);
                            stvSend.setVisibility(View.GONE);
                        }
                    }

                    tvName.setText(it.data.getNickname());
                    targetName = it.data.getNickname();
                    tvSexAge.setText(it.data.getAge() + "");
                    tvFun.setText(it.data.getFollowerCount().toString());
                    tvZan.setText(it.data.getLikedCount().toString());
                    tvCircleCount.setText(it.data.getCircleCount().toString());
                    loadImage(it.data.getAvatar(), siv_header);

//                    Glide.with(LookUserDetailActivity2.this)
//                            .load(it.data.getAvatar())
//                            .placeholder(R.mipmap.image_zanwei)
//                            .into(siv_header);
                    if (1 == it.data.getSex()) {
                        tvSexAge.setCompoundDrawablesWithIntrinsicBounds(
                                getResources().getDrawable(R.mipmap.icon_male),
                                null,
                                null,
                                null
                        );
                        tvSexAge.setBackgroundResource(R.drawable.shape_sex_male_bg);
                    } else {
                        tvSexAge.setCompoundDrawablesWithIntrinsicBounds(
                                getResources().getDrawable(R.mipmap.icon_male),
                                null,
                                null,
                                null
                        );
                        tvSexAge.setBackgroundResource(R.drawable.shape_sex_female_bg);
                    }
                    if (it.data.getVipLevel() == 0) {//不是会员
                        sll_vip.setVisibility(View.GONE);
                    } else {
                        sll_vip.setVisibility(View.VISIBLE);
                    }

                    List<String> list = new ArrayList<>();

                    if (!TextUtils.isEmpty(it.data.getUserNum())) {
                        list.add("ID：" + it.data.getUserNum().toString());
                    }
                    list.add(it.data.getAge() + "岁");
                    if (!TextUtils.isEmpty(it.data.getConstellation())) {
                        list.add(it.data.getConstellation().toString());
                    }
                    if (!TextUtils.isEmpty(it.data.getAddress())) {
                        list.add("现居" + it.data.getAddress());
                    }
                    if (!TextUtils.isEmpty(it.data.getMbti())) {
                        list.add(it.data.getMbti().toString());
                    }
                    if (!TextUtils.isEmpty(it.data.getHighSchool())) {
                        list.add(it.data.getHighSchool().toString());
                    }
                    if (!TextUtils.isEmpty(it.data.getIndustry())) {
                        list.add(it.data.getIndustry().toString());
                    }
                    tvIp.setLayoutManager(new FlexboxLayoutManager(getApplicationContext()));
                    tvIp.setAdapter(new TagAdapter(list));

                    banner_data.clear();
                    if (it.data.getPhotoList() != null && it.data.getPhotoList().size() > 0) {
                        banner_data.addAll(it.data.getPhotoList());
                    } else {
                        banner_data.add(it.data.getAvatar());
                    }
                    banner.setAdapter(new BannerImageAdapter<String>(banner_data) {
                        @Override
                        public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                            loadImage(data, holder.imageView);
                        }

                    });
                    // 开启循环轮播
                    banner.isAutoLoop(true);
                    banner.setIndicator(new CircleIndicator(getApplicationContext()));
                    //binding.tab1Banner.setScrollBarFadeDuration(3000)
                    // 设置指示器颜色
                    banner.setIndicatorSelectedColor(getResources().getColor(R.color.white));
                    banner.setIndicatorNormalColor(getResources().getColor(R.color.color_FF6300));
                    // 开始轮播
                    banner.start();
                }
            }
        });

        socialCircleViewModel.getToUserCommunityContentPage(IMManager.getInstance().getCurrentId(), targetId, pageNo, pageSize);
        socialCircleViewModel.getToUserCommunityContentPage().observe(this, new Observer<Result2<BaseResultListBean>>() {
            @Override
            public void onChanged(Result2<BaseResultListBean> result) {
                if (result.data != null) {
                    if (result.data.getList().size() > 0) {
                        pageNo = 1;
                        dataList.clear();
                        recyclerView.setVisibility(View.VISIBLE);
                        tvNone.setVisibility(View.GONE);
                        dataList.addAll(result.data.getList());
                        newData = result.data.getList();
                        listAdapter.setList(dataList);
                        listAdapter.notifyDataSetChanged();
                    } else {
                        recyclerView.setVisibility(View.GONE);
                        tvNone.setVisibility(View.VISIBLE);
                    }
                }

            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.stv_edit:
                Intent intentUserInfo = new Intent(this, EditUserInfoActivity.class);
                startActivity(intentUserInfo);
                break;
            case R.id.iv_back:
            case R.id.iv_back_black:
                finish();
                break;
            case R.id.iv_right_image:
            case R.id.iv_right_image_black:
                startActivity(new Intent(this, UserOperateActivity.class)
                        .putExtra(IntentExtra.STR_TARGET_ID, targetId)
                        .putExtra("contentId", "")
                        .putExtra("fromWhere", "private"));
                break;
            case R.id.stv_send://发消息
                RongIM.getInstance().startPrivateChat(this, targetId, targetName);
//                finish();
                break;
            case R.id.stv_add_black://按删除好友逻辑
                deleteFromContact();
                break;
            case R.id.stv_add_friend://添加好友
//                showAddFriendDialog(targetId);
                String inviteMsg = "";
                UserInfo userInfo =
                        RongUserInfoManager.getInstance()
                                .getUserInfo(IMManager.getInstance().getCurrentId());
                // 如果邀请信息为空则使用默认邀请语
                if (TextUtils.isEmpty(inviteMsg) && userInfo != null) {
                    // 当有附带群组名时显示来自哪个群组，没有时仅带自己的昵称
                    inviteMsg =
                            getString(
                                    R.string.profile_invite_friend_description_format,
                                    userInfo.getName());
                }
                viewModel.inviteFriend(targetId, inviteMsg);
                break;
            case R.id.stv_order://订阅
                String follower = "";
                if (isFollower.equals("1")) {
                    follower = "0";
                } else {
                    follower = "1";
                }
                HashMap<String, Object> map = new HashMap<>();
                map.put("followerUserId", targetId);
                map.put("isFollower", follower);//1:订阅 0：取消订阅
                map.put("userId", IMManager.getInstance().getCurrentId());
                viewProjectModel.createFollower(map, new Function1<BaseResultEntity<Object>, Unit>() {
                    @Override
                    public Unit invoke(BaseResultEntity<Object> result) {
                        if (result.getCode() == 200) {
                            userInfoViewModel.requestUserSelfInfo(targetId);
                        } else {
                            ToastUtils.showToast(result.getMsg());
                        }
                        return null;
                    }
                });
                break;
        }
    }

    @Override
    public void onItemClick(int position, @Nullable CommunityContent data) {
        imagesMediaList.clear();
        if (!TextUtils.isEmpty(data.getFiles())) {
            if (data.getFiles().contains(",")) {//多张

                for (int i = 0; i < MyUtils.toList(data.getFiles()).size(); i++) {
                    LocalMedia localMedia = new LocalMedia();
                    localMedia.setPath(MyUtils.toList(data.getFiles()).get(i));
                    imagesMediaList.add(localMedia);
                }
            } else {
                LocalMedia localMedia = new LocalMedia();
                localMedia.setPath(data.getFiles());
                imagesMediaList.add(localMedia);
            }
        }

        // 预览图片、视频、音频
        PictureSelector.create(this)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
                .isPreviewFullScreenMode(true)
//                .isPreviewZoomEffect(true, recyclerView)
                .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                    @Override
                    public void onPreviewDelete(int position) {

                    }

                    @Override
                    public boolean onLongPressDownload(Context context, LocalMedia media) {
                        return false;
                    }
                })
                .startActivityPreview(position, false, imagesMediaList);
    }

    @Override
    public void onItemLongClick(int position, @Nullable CommunityContent data) {

    }

    @Override
    public void onGroupClick(int position, @Nullable CommunityContent data) {
        if (IMManager.getInstance().getCurrentId().equals(data.getUserId() + "")) {
            RongIM.getInstance()
                    .startGroupChat(this, data.getGroupId() + "", data.getGroupsName());
        } else {
            JoinGroupViewModel joinGroupViewModel =
                    ViewModelProviders.of(
                                    this, new JoinGroupViewModel.Factory(getApplication(), data.getGroupId() + ""))
                            .get(JoinGroupViewModel.class);

            joinGroupViewModel.joinToGroup();
            // 获取加入群组结果
            joinGroupViewModel
                    .getJoinGroupInfo()
                    .observe(
                            this,
                            new Observer<Resource<Void>>() {
                                @Override
                                public void onChanged(Resource<Void> resource) {
                                    if (resource.status == Status.SUCCESS) {
                                        // 群组中包含自己则跳转到群聊天界面
                                        toGroupChat(data.getGroupId() + "", data.getGroupsName());
                                    } else if (resource.status == Status.ERROR) {
                                        if (resource.code == 430) {
//                                        ToastUtils.showToast("功能使用次数已达上限，请升级套餐或明天再试!");
                                            ToastUtils.showToast("该群成员已达上限，无法申请加入!");
                                        } else {
                                            ToastUtils.showToast(resource.code);
                                        }
                                    }
                                }
                            });
        }
    }

    private void toGroupChat(String groupId, String groupName) {
        Bundle bundle = new Bundle();
        bundle.putString("title", groupName);
        RouteUtils.routeToConversationActivity(
                this, ConversationIdentifier.obtainGroup(groupId), bundle);
        finish();
    }

    /**
     * 从通讯录中删除
     */
    private void deleteFromContact() {
        // 弹出删除好友确认对话框
        CommonDialog dialog =
                new CommonDialog.Builder()
                        .setContentMessage(
                                getString(
                                        R.string.profile_remove_from_contact_tips_html_format,
                                        targetName))
                        .setDialogButtonClickListener(
                                new CommonDialog.OnDialogButtonClickListener() {
                                    @Override
                                    public void onPositiveClick(View v, Bundle bundle) {
                                        // 标记正在删除好友
                                        isInDeleteAction = true;
                                        userDetailViewModel.deleteFriend(targetId);
                                        userDetailViewModel.addToBlackList();
                                    }

                                    @Override
                                    public void onNegativeClick(View v, Bundle bundle) {
                                    }
                                })
                        .build();
        dialog.show(getSupportFragmentManager(), null);
    }


    class TagAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        private List<String> list;

        public TagAdapter(@Nullable List<String> data) {
            super(R.layout.item_tag_dict, data);
            this.list = data;
        }


        @Override
        protected void convert(@NonNull BaseViewHolder holder, String item) {

            if (list.size() == 1) {
                ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab1_6));

            } else if (list.size() == 2) {
                switch (holder.getLayoutPosition()) {
                    case 0:
                        ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                        holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab1_6));
                        break;
                    case 1:
                        ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                        holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab2_6));
                        break;
                }

            } else if (list.size() == 3) {
                switch (holder.getLayoutPosition()) {
                    case 0:
                        ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                        holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab1_6));
                        break;
                    case 1:
                        ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                        holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab2_6));
                        break;
                    case 2:
                        ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                        holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab3_6));
                        break;
                }

            } else {
                if (holder.getLayoutPosition() == 0) {
                    ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                    holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab1_6));
                } else if (holder.getLayoutPosition() == 1) {
                    ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                    holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab2_6));
                } else if (holder.getLayoutPosition() == 2) {
                    ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                    holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab3_6));
                } else {
                    ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                    holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab4_6));
                }
                if (holder.getLayoutPosition() == list.size() - 1) {
                    ((TextView) holder.getView(R.id.stv_type)).setTextColor(getResources().getColor(R.color.color_333333));
                    holder.getView(R.id.stv_type).setBackground(getResources().getDrawable(R.drawable.shape_tab5_6));
                }
            }
            holder.setText(R.id.stv_type, item);
        }

    }


}